/*
 * Copyright 2002-2005 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.bingosoft.common.swato;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author Zhijie Chen (zigzag.chen@gmail.com)
 */
public class SpringContext implements ServiceContext {

	private static Log log = LogFactory.getLog(SpringContext.class);

	private static final String DEFAULT_SPRING_CONTEXT_FILE = "services.xml";

	private static BeanFactory m_ctx;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.swato.json_rpc.servlet.ServiceContext#setConfigFile(java.lang.String)
	 */
	public void setConfigFile(String filename) {
		log.info("Spring context init...");
		if (filename == null) {
			filename = DEFAULT_SPRING_CONTEXT_FILE;
		}
		m_ctx = new ClassPathXmlApplicationContext(filename);
		log.info("Spring context file setup completed.");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.swato.json_rpc.servlet.ServiceContext#getBean(java.lang.String)
	 */
	public Object getBean(String id) {
		return m_ctx.getBean(id);
	}

}
